{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 环比",
   "id": "1b1477cfbbb06566"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T12:00:18.170926Z",
     "start_time": "2025-09-15T12:00:17.477213Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "path = 'D:/2506A/monty03/day16/file/'"
   ],
   "id": "f75143c465913311",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T12:09:08.000730Z",
     "start_time": "2025-09-15T12:09:07.986382Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# df = pd.read_excel(path + '环比.xlsx')\n",
    "# # 计算环比 （绝对值的变化)\n",
    "# df['上月金额'] = df['金额'].shift(1)\n",
    "# df['环比变化'] = df['金额'] - df['上月金额']\n",
    "# df['环比增占率'] = (df['环比变化'] / df['上月金额'] % 100).round(2)\n",
    "# print(df)"
   ],
   "id": "eadbef9b5829688",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    城市  月份  金额  上月金额  环比变化  环比增占率\n",
      "0   北京   1  21   NaN   NaN    NaN\n",
      "1   北京   2  20  21.0  -1.0  99.95\n",
      "2   北京   3  23  20.0   3.0   0.15\n",
      "3   北京   4  17  23.0  -6.0  99.74\n",
      "4   北京   5  30  17.0  13.0   0.76\n",
      "5   北京   6  21  30.0  -9.0  99.70\n",
      "6   北京   7  19  21.0  -2.0  99.90\n",
      "7   北京   8  19  19.0   0.0   0.00\n",
      "8   北京   9  24  19.0   5.0   0.26\n",
      "9   北京  10  29  24.0   5.0   0.21\n",
      "10  北京  11  29  29.0   0.0   0.00\n",
      "11  北京  12  24  29.0  -5.0  99.83\n",
      "12  上海   1  22  24.0  -2.0  99.92\n",
      "13  上海   2  14  22.0  -8.0  99.64\n",
      "14  上海   3  20  14.0   6.0   0.43\n",
      "15  上海   4  21  20.0   1.0   0.05\n",
      "16  上海   5  30  21.0   9.0   0.43\n",
      "17  上海   6  19  30.0 -11.0  99.63\n",
      "18  上海   7  19  19.0   0.0   0.00\n",
      "19  上海   8  22  19.0   3.0   0.16\n",
      "20  上海   9  22  22.0   0.0   0.00\n",
      "21  上海  10  28  22.0   6.0   0.27\n",
      "22  上海  11  22  28.0  -6.0  99.79\n",
      "23  上海  12  29  22.0   7.0   0.32\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T12:14:48.409761Z",
     "start_time": "2025-09-15T12:14:48.402735Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# pct_change()直接计算环比增长率\n",
    "df['环比增长率'] = (df['金额'].pct_change(periods=1) *100).round(2)  #preiods 间隔期数\n",
    "df['环比变化'] = df['金额'].diff(periods=1)\n",
    "\n",
    "print(df)"
   ],
   "id": "3c28079e5dc95438",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    城市  月份  金额  上月金额  环比变化  环比增占率  环比增长率\n",
      "0   北京   1  21   NaN   NaN    NaN    NaN\n",
      "1   北京   2  20  21.0  -1.0  99.95  -4.76\n",
      "2   北京   3  23  20.0   3.0   0.15  15.00\n",
      "3   北京   4  17  23.0  -6.0  99.74 -26.09\n",
      "4   北京   5  30  17.0  13.0   0.76  76.47\n",
      "5   北京   6  21  30.0  -9.0  99.70 -30.00\n",
      "6   北京   7  19  21.0  -2.0  99.90  -9.52\n",
      "7   北京   8  19  19.0   0.0   0.00   0.00\n",
      "8   北京   9  24  19.0   5.0   0.26  26.32\n",
      "9   北京  10  29  24.0   5.0   0.21  20.83\n",
      "10  北京  11  29  29.0   0.0   0.00   0.00\n",
      "11  北京  12  24  29.0  -5.0  99.83 -17.24\n",
      "12  上海   1  22  24.0  -2.0  99.92  -8.33\n",
      "13  上海   2  14  22.0  -8.0  99.64 -36.36\n",
      "14  上海   3  20  14.0   6.0   0.43  42.86\n",
      "15  上海   4  21  20.0   1.0   0.05   5.00\n",
      "16  上海   5  30  21.0   9.0   0.43  42.86\n",
      "17  上海   6  19  30.0 -11.0  99.63 -36.67\n",
      "18  上海   7  19  19.0   0.0   0.00   0.00\n",
      "19  上海   8  22  19.0   3.0   0.16  15.79\n",
      "20  上海   9  22  22.0   0.0   0.00   0.00\n",
      "21  上海  10  28  22.0   6.0   0.27  27.27\n",
      "22  上海  11  22  28.0  -6.0  99.79 -21.43\n",
      "23  上海  12  29  22.0   7.0   0.32  31.82\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 同比",
   "id": "61a1b8618b0fed39"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T12:22:10.278262Z",
     "start_time": "2025-09-15T12:22:10.098818Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '同比.xlsx')\n",
    "year = df['日期'].dt.year\n",
    "\n",
    "df2 = pd.pivot_table(df, values='金额', index='店号', columns=year,aggfunc='sum')\n",
    "df2['同比'] = (df2[2019] - df2[2018]) / df2[2018]\n",
    "print(df2)\n"
   ],
   "id": "2a7d02a4b94f38f1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "日期    2018  2019        同比\n",
      "店号                        \n",
      "A001  7400  7293 -0.014459\n",
      "B001  9086  9163  0.008475\n"
     ]
    }
   ],
   "execution_count": 16
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
